package com.jwdf.uda.ui.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Toast;

import com.jwdf.uda.R;
import com.jwdf.uda.event.HandleInvalidEvent;
import com.jwdf.uda.event.LongTaskEvent;
import com.jwdf.uda.event.MessageNoticeEvent;
import com.jwdf.uda.event.UserEvent;
import com.jwdf.uda.ui.fragment.garb.FindFragment;
import com.jwdf.uda.ui.fragment.home.HomeFragment;
import com.jwdf.uda.ui.fragment.question.QuizFragment;
import com.jwdf.uda.ui.fragment.user.UserFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ShareUtil;
import com.jwdf.uda.utils.StatisticalUtil;
import com.jwdf.uda.view.statusbar.StatusBarCompat;
import com.jwdf.uda.view.tabstrip.APSTSViewPager;
import com.jwdf.uda.view.tabstrip.AdvancedPagerSlidingTabStrip;
import com.umeng.message.PushAgent;

/**
 * Created by Administrator on 2016/5/18.
 */
public class DefaultMainActivity extends AbsActivity {
    protected static final int HOME_TAB_INDEX = 0;
    protected static final int FIND_TAB_INDEX = 2;
    protected static final int QUIZ_TAB_INDEX = 1;
    protected static final int INDIVIDUAL_TAB_INDEX = 3;
    protected static final int TAB_COUNT = 4;
    private static final int DELAY = 2000;
    public AdvancedPagerSlidingTabStrip mTabStrip;
    public APSTSViewPager mViewPager;
    private boolean mIsBackPressed = false;
    private HomeFragment mHomeFragment;
    private FindFragment mFindFragment;
    private QuizFragment mQuizFragment;
    private UserFragment mUserFragment;

    private Handler mExitHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            mIsBackPressed = false;
        }

    };

    private ViewGroup getRootView() {
        return (ViewGroup) View.inflate(this, getLayoutIdRes(), null);
    }

    protected int getLayoutIdRes() {
        return R.layout.fragment_individual;
    }

    @Override
    protected boolean fullScreenMode() {
        return true;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
        PushAgent.getInstance(this).onAppStart();
        setContentView(getLayoutIdRes());
        setupContentView();
    }

    private void setupContentView() {
        initOtherViews();
        mTabStrip = (AdvancedPagerSlidingTabStrip) findViewById(R.id.tabs);
        mViewPager = (APSTSViewPager) findViewById(R.id.vp_main);
        mViewPager.setOffscreenPageLimit(TAB_COUNT);
        FragmentStatePagerAdapter adapter = getPagerAdapter();

        mViewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mTabStrip.setViewPager(mViewPager);
        mViewPager.setCurrentItem(HOME_TAB_INDEX);
    }

    protected void initOtherViews() {
        StatusBarCompat.setStatusBarColor(this,false, true,getResources().getColor(R.color.colorPrimary));
    }

    protected FragmentStatePagerAdapter getPagerAdapter() {
        return new DefaultFragmentPagerAdapter(getSupportFragmentManager());
    }

    @Override
    protected void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        ShareUtil.stop(this);
        ConfigUtils.setHandledInvalid(getApplicationContext(), false);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            FragmentManager manager = getSupportFragmentManager();
            int count = manager.getBackStackEntryCount();
            if (count == 0) {
                mExitHandler.removeMessages(0);
                mExitHandler.sendEmptyMessageDelayed(0, DELAY);
                if (!mIsBackPressed) {
                    mIsBackPressed = true;
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_LONG).show();
                    return true;
                }
            }
        }
        EventBusUtil.post(new LongTaskEvent(LongTaskEvent.LONG_TASK_FINISH));
        return super.onKeyDown(keyCode, event);
    }

    public void onEventMainThread(UserEvent event) {
        if (UserEvent.EVENT_LOGOUT == event.getEvent()) {
            if (TextUtils.equals(getString(R.string.android_force_update_dialog_btn_cancel), event.getExtra())) {
                finish();
            } else {
                MainLoginEntryActivity.start(this);
                finish();
            }
        }
    }

    public void onEventMainThread(HandleInvalidEvent event) {
        MainLoginEntryActivity.start(this);
        finish();
    }

    public void onEventMainThread(MessageNoticeEvent event) {
        if (MessageNoticeEvent.USER_TAB_TIPS == event.getEvent()) {
            int msgCount = event.getCount();
            if (msgCount > 0) {
                mTabStrip.showDot(INDIVIDUAL_TAB_INDEX, "" + msgCount);
            } else {
                mTabStrip.hideDot(INDIVIDUAL_TAB_INDEX);
            }
        }
    }

    protected class DefaultFragmentPagerAdapter extends FragmentStatePagerAdapter implements AdvancedPagerSlidingTabStrip.IconTabProvider {

        public DefaultFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position >= 0 && position < TAB_COUNT) {
                switch (position) {
                    case HOME_TAB_INDEX:
                        if (null == mHomeFragment)
                            mHomeFragment = HomeFragment.instance();
                        return mHomeFragment;

                    case FIND_TAB_INDEX:
                        if (null == mFindFragment)
                            mFindFragment = FindFragment.instance();
                        return mFindFragment;

                    case QUIZ_TAB_INDEX:
                        if (null == mQuizFragment)
                            mQuizFragment = QuizFragment.instance();
                        return mQuizFragment;

                    case INDIVIDUAL_TAB_INDEX:
                        if (null == mUserFragment)
                            mUserFragment = UserFragment.instance();
                        return mUserFragment;
                    default:
                        break;
                }
            }
            return null;
        }

        @Override
        public int getCount() {
            return TAB_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position >= 0 && position < TAB_COUNT) {
                switch (position) {
                    case HOME_TAB_INDEX:
                        return "首页";
                    case FIND_TAB_INDEX:
                        return "发现";
                    case QUIZ_TAB_INDEX:
                        return "问答";
                    case INDIVIDUAL_TAB_INDEX:
                        return "我的";
                    default:
                        break;
                }
            }
            return null;
        }

        @Override
        public Integer getPageIcon(int index) {
            if (index >= 0 && index < TAB_COUNT) {
                switch (index) {
                    case HOME_TAB_INDEX:
                        return R.mipmap.icon_tab_home_n;
                    case FIND_TAB_INDEX:
                        return R.mipmap.icon_tab_find_n;
                    case QUIZ_TAB_INDEX:
                        return R.mipmap.icon_tab_garb_n;
                    case INDIVIDUAL_TAB_INDEX:
                        return R.mipmap.icon_tab_me_n;
                    default:
                        break;
                }
            }
            return 0;
        }

        @Override
        public Integer getPageSelectIcon(int index) {
            if (index >= 0 && index < TAB_COUNT) {
                switch (index) {
                    case HOME_TAB_INDEX:
                        return R.mipmap.icon_tab_home;
                    case FIND_TAB_INDEX:
                        return R.mipmap.icon_tab_find;
                    case QUIZ_TAB_INDEX:
                        return R.mipmap.icon_tab_garb;
                    case INDIVIDUAL_TAB_INDEX:
                        return R.mipmap.icon_tab_me;
                    default:
                        break;
                }
            }
            return 0;
        }

        @Override
        public Rect getPageIconBounds(int position) {
//           return new Rect(0, 0, mSize+1, mSize);
            return null;
        }
    }

}
